<template>
    <div>

        <h1>医生信息添加</h1>
        <a href="/showys">医生信息</a> 
        <br>
        <table class="table">
            <tbody>
                <tr>
                    <td>姓名</td>
                    <td><input type="text" v-model="data.doctorName"></td>
                </tr>
                <tr>
                    <td>所属科室</td>
                    <td>
                       <select v-model="data.departid">
            <option value="0">请选择科室</option>
            <option v-for="e in KSxlk" :value="e.id">{{ e.departName }}</option>
        </select> 
                    </td>
                </tr>
                <tr>
                    <td>头像</td>
                    <td>
                        <input type="file" @change="imgurl">
                        <img :src="'https://localhost:7125/'+data.imgUrl" style="height: 60px; width: 60px;">
                    </td>
                </tr>
                <tr>
                    <td>创建日期</td>
                    <td>
                        <input type="date" v-model="data.doctorDate">
                    </td>
                </tr>
                <tr>
                    <td>
                        <input type="button" value="添加" @click="add">
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import {ref,onMounted} from 'vue'
import axios from 'axios'
import { useRouter } from 'vue-router'

const router=useRouter();
onMounted(()=>{ 
XLKS(); 
})

//上传
const imgurl=(e:any)=>{
   var f = e.target.files[0];
   var fs = new FormData();
   fs.append("file",f);
   debugger

   if(f.size>1024*1024*2)
   {
     alert("图片大小不能超过2MB");
     return;
   } 

   axios.post("/api/ImgUrl",fs).then(res=>{
    data.value.imgUrl=res.data
   })
}

//科室下拉框
const KSxlk=ref([{
    "id": 1,
    "departName": "内科",
}])
const XLKS=()=>{
    axios.get("/api/Depart/Showxlk").then(res=>{
        KSxlk.value=res.data
    })
}


const data =ref({
     "id": 0,
  "doctorName": "",
  "departid": 0,
  "imgUrl": "",
  "doctorDate": "2025-05-14T01:11:54.202Z"
})

const add=()=>{
    if(data.value.doctorName=="")
{
    alert("姓名不能为空");
    return;
}
    axios.post("/api/Doctor/AddDoctor",data.value).then(res=>{
        if(res.data>0)
    {
        alert("添加成功");
        router.push({path:"/showys"})
    }
    else{
        alert("添加失败");
        return;
    }
    })
}

</script>

<style scoped>

</style>